from microbit import *

def lightLevel():
    _sav = display.is_on()
    display.off()

    pin10.write_digital(1)
    sleep(1)
    r = pin10.read_analog()
    sleep(2)
    t = 0
    for i in range(48):
        t += r - pin10.read_analog()
    pin10.write_digital(1)

    if _sav:
        display.on()

    t -= 700
    t = max(0, min(t, 255))

    return t

while 1:
    
    n = lightLevel()
    uart.write(str(n)+'\r\n')

    sleep(200)
